// pages/business/mytrade/mytrade.js
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 10,
    listflag: true,
    agentflag: true,
    isfresh: false,
    showLoading: true,
    agent_trade: [],
    currentTab: 1,
    currentId: "0",
    res_navbar: [
      {
        "name": "全部",
        "id": "",
        "word": "暂无代理商进货哦！快去联系他们吧~"
      },
      {
        "name": "待付款",
        "id": "0",
        "word": "暂无待付款进货单！快去联系代理商吧~"
      },
      {
        "name": "已付款",
        "id": "1",
        "word": "暂无已付款进货单！快去联系代理商吧~"
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.agentTradeList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.isfresh) {
      this.setData({
        isfresh: false
      })
      this.refreshlist();
    }
  },
  onResNavbarTap: function (e) {
    this.data.agent_trade.length = 0;
    this.setData({
      page: 1,
      page_size: 10,
      listflag: true,
      showLoading: true,
      agent_trade: [],
      currentId: e.currentTarget.dataset.id,
      currentTab: e.currentTarget.dataset.index
    });
    this.agentTradeList();
  },
  agentTradeList: function() {
    var that = this;
    shihan.sendRequest(config.agent_child_trade_list, 'POST', {
        page: that.data.page,
        page_size: that.data.page_size,
        status: that.data.currentId
      })
      .then(function(response) {
        if (response.data.code == 0) {
          // console.log(response);
          if (response.data.data.sh_items.length > 0) {
            that.setData({
              page: that.data.page + 1,
              agent_trade: that.data.agent_trade.concat(response.data.data.sh_items),
              showLoading: false,
              listflag: true,
              agentflag: true
            })
          } else {
            that.setData({
              showLoading: false,
              listflag: false,
              agentflag: false
            })
          }
          if (that.data.agent_trade.length == response.data.data.sh_total_rows) {
            that.setData({
              showLoading: false,
              listflag: false,
              agentflag: false
            })
          }
        } else {
          that.setData({
            showLoading: false,
            listflag: false,
            agentflag: false
          })
        }
        wx.stopPullDownRefresh();
      }, function(error) {
        console.log(error);
      });
  },
  orderDetail: function(e) { //订单详情
    wx.navigateTo({
      url: '/pages/business/myorderdetail/myorderdetail?index=1&tradeno=' + e.currentTarget.dataset.trade,
    })
  },
  payTrade: function(e) { //我的进货单列表-设置为已付款
    var that = this,
      orderno = e.currentTarget.dataset.trade;
    wx.showModal({
      title: "提示",
      content: '请确认您已收到货款',
      confirmText: "确认",
      confirmColor: "#c40000",
      showCancel: true,
      success: function(res) {
        if (res.confirm) {
          shihan.sendRequest(config.agent_child_trade_paid, 'POST', {
              trade_no: orderno
            })
            .then(function(response) {
              if (response.data.code == 0) {
                that.refreshlist();
              } else {
                shihan.showToast.call(that, response.data.msg);
              }
            }, function(error) {
              console.log(error);
            });
        }
      }
    })
  },
  refreshlist: function() {
    var that = this;
    that.data.agent_trade.length = 0;
    that.setData({
      page: 1,
      page_size: 10,
      listflag: true,
      showLoading: true,
      agent_trade: [],
    });
    that.agentTradeList();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.refreshlist();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    if (that.data.agentflag) {
      that.setData({
        agentflag: false
      })
      this.agentTradeList();
    }
  }
})